package com.ddwl.user.service;


import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.user.JoinerApproveBo;
import com.ddwl.schema.bo.user.JoinerBo;
import com.ddwl.schema.bo.user.QueryJoinerBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.user.JoinerVo;
import com.ddwl.user.dao.JoinerDao;
import com.ddwl.user.dao.mapper.JoinerMapper;
import com.ddwl.user.dao.model.Joiner;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 招商service
 * @author wenbin
 */
@Service
public class JoinerService {

    @Resource
    private JoinerDao joinerDao;

    @Resource
    private JoinerMapper joinerMapper;

    @Transactional(rollbackFor = Throwable.class)
    public String create(JoinerBo bo) {
        Joiner entity = new Joiner();
        BeanUtils.copyProperties(bo, entity);

        //创建或更新商品
        if (entity.getId() != null && !entity.getId().isEmpty()) {
            joinerDao.updateByPrimaryKeySelective(entity);
        } else {
            Joiner joiner = new Joiner();
            joiner.setJoinerTel(bo.getJoinerTel());
            joiner.setJoinerDk(bo.getJoinerDk());
            Joiner joiner1 = joinerDao.selectOne(joiner);
            if (joiner1==null){
                entity.setId(UUIDGenerator.getShortUUID());
                joinerDao.insert(entity);
            }else {
                entity.setId(joiner1.getId());
                joinerDao.updateByPrimaryKeySelective(entity);
            }
        }
        return entity.getId();
    }

    @Transactional(rollbackFor = Throwable.class)
    public void approve(JoinerApproveBo bo) {
        Joiner entity = new Joiner();
        BeanUtils.copyProperties(bo, entity);
        ParamException.isBlank(entity.getId(),"用户id为空");
        ParamException.isNull(entity.getApproveStatus(),"用户审批状态为空");
        //创建或更新商品
        if (entity.getId() != null && !entity.getId().isEmpty()) {
            Joiner joiner = joinerDao.selectByPrimaryKey(entity.getId());
            if (joiner!=null){
                if(joiner.getApproveStatus()==null)joiner.setApproveStatus(0);
                ParamException.isTrue(joiner.getApproveStatus()==3,"用户已经被拒绝,请重新申请");
                ParamException.isTrue(joiner.getApproveStatus()==1,"用户已经审批通过,不能修改审批状态");
                joinerDao.updateByPrimaryKeySelective(entity);
            }
        }
    }

    /**
     * 查询加盟用户申请列表
     *
     * @param pageBo
     * @return
     */
    public PageVo<JoinerVo> list(PageBo<QueryJoinerBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(),pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(),pageBo.isAsc()));
        QueryJoinerBo bo = pageBo.getParam();
        Joiner entity = new Joiner();
        BeanUtils.copyProperties(bo, entity);
        List<Joiner> entities = joinerDao.select(entity);
        PageInfo<Joiner> info = new PageInfo(entities);
        List<JoinerVo>joinerVos= BeanConverUtils.convertBeanList(entities,JoinerVo.class);
        return new PageVo<>(info.getTotal(),pageBo.getPage(),pageBo.getPageSize(),joinerVos);
    }



}
